﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using AutoWelding.BLL;
using AutoWelding.Utils;

using log4net;

namespace AutoWelding
{
   
    public partial class LoginForm : Form
    {
        //private static readonly ILog log = LogManager.GetLogger(typeof(LoginForm));
        public LoginForm()
        {
            InitializeComponent();
        }
        private LoginBLL loginBLL  = new LoginBLL();
        /// <summary>
        /// 检查用户是否存在（登陆验证）
        /// </summary>
        public void Login()
        {
            string username = txt_username.Text;
            string password = txt_password.Text;
            if(loginBLL.CheckUser(username, password) != null)
            {
                MainForm mainForm = new MainForm();
                mainForm.Tag = loginBLL.CheckUser(username, password);
                mainForm.Show();
                this.Hide();
                LogHelper.Info(typeof(LoginForm),"用户：" + username + " 登陆成功！");
            }
            else
            {
                MessageBox.Show("用户名或密码错误或该账号已被禁用，请联系管理员！");
            }
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("是否退出系统?", "退出系统", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Application.ExitThread();
            }
            else
                e.Cancel = true;
        }

        private bool isShow = false;
        /// <summary>
        /// 密码显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            isShow = !isShow;
            if (isShow)
            { 
                txt_password.PasswordChar = '\0';//显示明文
                pictureBox1.Image = Properties.Resources.Invisible32;
            }else
            {
                txt_password.PasswordChar = '*';//隐藏
                pictureBox1.Image = Properties.Resources.Visible32;
            }
        }

        private void txt_password_KeyDown(object sender, KeyEventArgs e)
        {
            //当光标位于密码时 如果用户按下了键盘上的Enter键 调用登录方法
            if (e.KeyCode == Keys.Enter)
            {
                Login();
            }
        }
    }
}
